package org.jfinanceobserver.persistence.jpa.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.jfinanceobserver.persistence.jpa.controller.exceptions.IllegalOrphanException;
import org.jfinanceobserver.persistence.jpa.controller.exceptions.NonexistentEntityException;
import org.jfinanceobserver.persistence.jpa.entities.QuotesActive;
import org.jfinanceobserver.persistence.jpa.entities.QuotesData;

/**
 * This class is the QuotesActive Jpa Controller
 *
 * $Author: r78v10a07@gmail.com $ 
 * $LastChangedDate: 2013-02-12 09:59:23 +0000 (Tue, 12 Feb 2013) $ 
 * $LastChangedRevision: 19 $
 * @since Feb 11, 2013
 */
public class QuotesActiveJpaController extends AbstractJpaController<QuotesActive> implements Serializable {

    public QuotesActiveJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    @Override
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(QuotesActive quotesActive) {
        if (quotesActive.getQuotesDataCollection() == null) {
            quotesActive.setQuotesDataCollection(new ArrayList<QuotesData>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<QuotesData> attachedQuotesDataCollection = new ArrayList<QuotesData>();
            for (QuotesData quotesDataCollectionQuotesDataToAttach : quotesActive.getQuotesDataCollection()) {
                quotesDataCollectionQuotesDataToAttach = em.getReference(quotesDataCollectionQuotesDataToAttach.getClass(), quotesDataCollectionQuotesDataToAttach.getId());
                attachedQuotesDataCollection.add(quotesDataCollectionQuotesDataToAttach);
            }
            quotesActive.setQuotesDataCollection(attachedQuotesDataCollection);
            em.persist(quotesActive);
            for (QuotesData quotesDataCollectionQuotesData : quotesActive.getQuotesDataCollection()) {
                QuotesActive oldActiveidOfQuotesDataCollectionQuotesData = quotesDataCollectionQuotesData.getActiveid();
                quotesDataCollectionQuotesData.setActiveid(quotesActive);
                quotesDataCollectionQuotesData = em.merge(quotesDataCollectionQuotesData);
                if (oldActiveidOfQuotesDataCollectionQuotesData != null) {
                    oldActiveidOfQuotesDataCollectionQuotesData.getQuotesDataCollection().remove(quotesDataCollectionQuotesData);
                    oldActiveidOfQuotesDataCollectionQuotesData = em.merge(oldActiveidOfQuotesDataCollectionQuotesData);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(QuotesActive quotesActive) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            QuotesActive persistentQuotesActive = em.find(QuotesActive.class, quotesActive.getId());
            Collection<QuotesData> quotesDataCollectionOld = persistentQuotesActive.getQuotesDataCollection();
            Collection<QuotesData> quotesDataCollectionNew = quotesActive.getQuotesDataCollection();
            List<String> illegalOrphanMessages = null;
            for (QuotesData quotesDataCollectionOldQuotesData : quotesDataCollectionOld) {
                if (!quotesDataCollectionNew.contains(quotesDataCollectionOldQuotesData)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain QuotesData " + quotesDataCollectionOldQuotesData + " since its activeid field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<QuotesData> attachedQuotesDataCollectionNew = new ArrayList<QuotesData>();
            for (QuotesData quotesDataCollectionNewQuotesDataToAttach : quotesDataCollectionNew) {
                quotesDataCollectionNewQuotesDataToAttach = em.getReference(quotesDataCollectionNewQuotesDataToAttach.getClass(), quotesDataCollectionNewQuotesDataToAttach.getId());
                attachedQuotesDataCollectionNew.add(quotesDataCollectionNewQuotesDataToAttach);
            }
            quotesDataCollectionNew = attachedQuotesDataCollectionNew;
            quotesActive.setQuotesDataCollection(quotesDataCollectionNew);
            quotesActive = em.merge(quotesActive);
            for (QuotesData quotesDataCollectionNewQuotesData : quotesDataCollectionNew) {
                if (!quotesDataCollectionOld.contains(quotesDataCollectionNewQuotesData)) {
                    QuotesActive oldActiveidOfQuotesDataCollectionNewQuotesData = quotesDataCollectionNewQuotesData.getActiveid();
                    quotesDataCollectionNewQuotesData.setActiveid(quotesActive);
                    quotesDataCollectionNewQuotesData = em.merge(quotesDataCollectionNewQuotesData);
                    if (oldActiveidOfQuotesDataCollectionNewQuotesData != null && !oldActiveidOfQuotesDataCollectionNewQuotesData.equals(quotesActive)) {
                        oldActiveidOfQuotesDataCollectionNewQuotesData.getQuotesDataCollection().remove(quotesDataCollectionNewQuotesData);
                        oldActiveidOfQuotesDataCollectionNewQuotesData = em.merge(oldActiveidOfQuotesDataCollectionNewQuotesData);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = quotesActive.getId();
                if (findQuotesActive(id) == null) {
                    throw new NonexistentEntityException("The quotesActive with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            QuotesActive quotesActive;
            try {
                quotesActive = em.getReference(QuotesActive.class, id);
                quotesActive.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The quotesActive with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<QuotesData> quotesDataCollectionOrphanCheck = quotesActive.getQuotesDataCollection();
            for (QuotesData quotesDataCollectionOrphanCheckQuotesData : quotesDataCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This QuotesActive (" + quotesActive + ") cannot be destroyed since the QuotesData " + quotesDataCollectionOrphanCheckQuotesData + " in its quotesDataCollection field has a non-nullable activeid field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(quotesActive);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<QuotesActive> findQuotesActiveEntities() {
        return findQuotesActiveEntities(true, -1, -1);
    }

    public List<QuotesActive> findQuotesActiveEntities(int maxResults, int firstResult) {
        return findQuotesActiveEntities(false, maxResults, firstResult);
    }

    private List<QuotesActive> findQuotesActiveEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(QuotesActive.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public QuotesActive findQuotesActive(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(QuotesActive.class, id);
        } finally {
            em.close();
        }
    }

    public int getQuotesActiveCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<QuotesActive> rt = cq.from(QuotesActive.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
